package com.hujing.io;

import java.io.*;
import java.nio.charset.Charset;

/**
 * @Author : hujing
 * @Date: 2025/7/22 15:00
 * @Description: 文件输入流
 */
public class FileInputStreamDemo {

    public static void main(String[] args) {
        // 方式一
//        File file = new File("F:\\project\\spring-parent\\io\\src\\main\\resources\\知识搜索.txt");
//        try (FileInputStream fis = new FileInputStream(file);
//             InputStreamReader isr = new InputStreamReader(fis);
//             BufferedReader br = new BufferedReader(isr)
//        ) {
//            String line;
//            while ((line = br.readLine()) != null) {
//                System.out.println(line);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        
        // 方式二
        try(InputStream is = FileInputStreamDemo.class.getClassLoader().getResourceAsStream("知识搜索.txt");
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(isr)){
            String line;
            while ((line=br.readLine())!=null){
                System.out.println(line);
            }
        }catch (Exception e){
            e.printStackTrace();;
        }
    }
}
